/*
 * author: wangx
 */
 $(document).ready(function(){
   removeNav();
 });
//申请物资列表
requestApplyForRe();

function requestApplyForRe() {
  var el = {};
  requestWithSession(el, hostUrl + '/resource/applylist', function(data) {
      var box = document.getElementsByClassName('applyList')[0];
      var list = data.data;
      var str = '';
      for (var i = 0; i < list.length; i++) {
        str += "<span class='applyUserId'>" + list[i].username +
          "</span><span class='applyThName'>" + list[i].resourcename +
          "</span><span class='agreeApply'><button value='" + list[i].resourceid +
          "' class='applyBtn' onclick='agreeApplyResource(this.value)'>同意</button></span>" +
          "<span class='refuseApply'><button value='" + list[i].resourceid + "'class='applyBtn' onclick='refuseApplyResource(this.value)'>拒绝</button></span>";
      }
      box.innerHTML = str;
    },
    function(error) {
      console.log(error);
    }, true);
}
//同意申请
function agreeApplyResource(id) {
  var el = {};
  el.resourceid = id;
  el.ischeck = 1;
  requestWithSession(el, hostUrl + '/resource/check', function(data) {
      if (data.status == 0) {
        alert('审核通过!');
      } else {
        alert('审核拒绝!');
      }
      requestApplyForRe();
    },
    function(error) {
      console.log(error);
    });
}
//拒绝申请
function refuseApplyResource(id) {
  var el = {};
  el.resourceid = id;
  el.ischeck = 0;
  requestWithSession(el, hostUrl + '/resource/check', function(data) {
      if (data.status == 0) {
        alert('已拒绝!');
      } else {
        alert('失败!');
      }
      requestApplyForRe();
    },
    function(error) {
      console.log(error);
    });
}

//请求已借物资列表
requestBorrowList();
function requestBorrowList(){
  var el = {};
  requestWithSession(el,hostUrl + '/resource/borrowlist',function(data){
    var box = document.getElementsByClassName('borrowInfoList')[0];
    var box2 = document.getElementsByClassName('unreturnInfoList')[0];
    var list = data.data;
    var str = '';
    var str2 = '';
    var nowDate = new Date();
    for(var i = 0;i<list.length ;i++){
      var bdate = new Date(list[i].borrowdate);
      var rdate = new Date(list[i].returndate);
      var bdatey = bdate.getFullYear(),
          bdatem = bdate.getMonth() + 1;
          bdated = bdate.getDate(),
          rdatey = rdate.getFullYear(),
          rdatem = rdate.getMonth() + 1;
          rdated = rdate.getDate();
      if(nowDate > rdate){
        str2 += "<span class='unreturnUserId'>" + list[i].username +
        "</span><span class='unreturnThName'>" + list[i].resourcename +
        "</span><span class='unreturnBTime'>" + bdatey + "-" +
        bdatem + "-" + bdated +
        "</span><span class='unreturnRTime'>" + rdatey + "-" +
        rdatem + "-" + rdated +
        "</span><span class='unreturnBorrow'><button value='" + list[i].resourceid +
        "'class='unreturnBtn' onclick='confirmReturnRe(this.value)'>确认归还</button></span>";
      }else {
        str += "<span class='borrowUserId'>" + list[i].username +
        "</span><span class='borrowThName'>" + list[i].resourcename +
        "</span><span class='borrowTime'>" + bdatey + "-" +
        bdatem + "-" + bdated +
        "</span><span class='returnTime'>" + rdatey + "-" +
        rdatem + "-" + rdated +
        "</span><span class='agreeBorrow'><button value='" + list[i].resourceid +
        "'class='returnBtn' onclick='confirmReturnRe(this.value)'>确认归还</button></span>";
      }
    }
    box.innerHTML = str;
    box2.innerHTML = str2;
  },
  function(error){
    console.log(error);
  });
}
//确认归还物资
function confirmReturnRe(resourceid){
  var el = {};
  el.resourceid = resourceid;
  requestWithSession(el,hostUrl+'/resource/getback',function(data){
    if(data.status == 0){
      alert('操作成功!');
    }else {
      alert('操作失败!');
    }
    requestBorrowList();
  },
  function(error){
    console.log(error);
  });
}
// 请求实验室闲置物资进行展示
requestUnUesdRs();
function requestUnUesdRs() {
  var el = {};
  requestWithSession(el, hostUrl + '/resource/unusedresourcelist', function(data) {
      var box = document.getElementsByClassName('unusedList')[0];
      var str = '';
      var list = data.data;
      for (var i = 0; i < list.length; i++) {
        str += "<span class='unusedThName'>" + list[i].resourcename +
          "</span><span class='unusedThinfo'>" + list[i].content + "</span>";
      }
      box.innerHTML = str;
    },
    function(error) {
      console.log(error);
    });
}
